//
// Created by Administrator on 2022-11-29.
//
#include "CCRender.h"

CCRender::CCRender() {

}

CCRender::~CCRender(){

}

/**
 * 加载
 */
void CCRender::initGL()
{
    //清空颜色
    glClearColor(0.0,0,0,1.0);
    //清空深度缓存
    glClearDepthf(1.0);
    //开启深度测试
    glEnable(GL_DEPTH_TEST);
    //什么情况下使用深度测试
    glDepthFunc(GL_LEQUAL);
}
/**
 * 绘制
 */
void CCRender::paintGL()
{
    //清空颜色缓冲区或深度缓冲区
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    drawTriangle();
}

/**
 * 画三角
 */
void CCRender::drawTriangle() {

    CCFloat7 triangleVert[] ={
            {0,       0.5,    -3,  1,  0,  0,1.0},
            {-0.5,   -0.5,    -3,  0,  1,  0,1.0},
            {0.5,    -0.5,    -3,  0,  0,  1,1.0},
    };


    m_angle += 0.03f;

    glm::mat4x4  cubeMat;
    glm::mat4x4  cubeTransMat = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -3));
    glm::mat4x4  cubeRotMat = glm::rotate(glm::mat4(1.0f),m_angle,glm::vec3(0.0f, 0.0f, -3.0) );
    glm::mat4x4  cubeScaleMat = glm::scale(glm::mat4(1.0f),glm::vec3(0.3f, 0.2f, 0.3) );

    //透视投影矩阵，跟之前的glOrthof(-1,1,-1,1,0.1,1000);差不多意思，3.0glOrthof这个API没有了
    glm::mat4 projMat = glm::perspective(glm::radians(60.0f), (float)9/(float)18, 0.0f, 1000.0f);
    cubeMat = projMat * cubeRotMat;

    ccOpenGlShader.Bind();

    ccOpenGlShader.SetUniformValue("u_mat",cubeMat);

    ccOpenGlShader.EnableAttributeArray("a_position");
    ccOpenGlShader.EnableAttributeArray("a_color");

    ccOpenGlShader.SetAttributeBuffer("a_position",GL_FLOAT,triangleVert,3,sizeof(CCFloat7));
    ccOpenGlShader.SetAttributeBuffer("a_color",GL_FLOAT,&triangleVert[0].r,4,sizeof(CCFloat7));

    glDrawArrays(GL_TRIANGLES,0,3);

    ccOpenGlShader.DisableAttributeArray("a_position");
    ccOpenGlShader.DisableAttributeArray("a_color");

    ccOpenGlShader.Release();
}


/**
 * 窗口大小改变
 */
void CCRender::resizeGL(int width,int height)
{
    //设置窗口大小
    glViewport(0,0,width,height);
}
